layui.use(['table', 'admin', 'ax','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 自有车档案表管理
     */
    var BasicPrivatecar = {
        tableId: "basicPrivatecarTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPrivatecar.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', title: 'ID', hide: true},
            {align: 'center', toolbar: '#tableBar', title: 'Operations',width:150},
            {field: 'privatecarCarnumber', title: 'License Plate', style:'font-weight: 600;'},
            {field: 'privatecarState', title: 'Status',templet: '#billstateTpl'},
            {field: 'operateName', title: 'Project', style:'font-weight: 600;'},
            {field: 'privatecarFleetName', title: 'Carrier', style:'font-weight: 600;'},
            {field: 'privatecarNumber', title: 'Number', style:'font-weight: 600;', hide: true},

            {field: 'driver1', title: 'Primary Driver', style:'font-weight: 600;'},
            {field: 'tel1', title: 'Phone', style:'font-weight: 600;'},

            {field: 'privatecarColour', title: 'Plate Color', style:'font-weight: 600;'},
            {field: 'chengyunleixing', title: 'Carrier Type', style:'font-weight: 600;'},
            {field: 'privatecarVhiclegenus', title: 'Vehicle Type', style:'font-weight: 600;'},
            {field: 'pinpaileixing', title: 'Brand Type', style:'font-weight: 600;', hide: true},
            {field: 'cheliangleibie', title: 'Vehicle Category', style:'font-weight: 600;'},
            {field: 'privatecarType', title: 'Model', style:'font-weight: 600;'},
            {field: 'privatecarCarlong', title: 'Length(m)', style:'font-weight: 600;'},
            {field: 'privatecarCarwide', title: 'Width(m)', style:'font-weight: 600;'},
            {field: 'privatecarCarhigh', title: 'Height(m)', style:'font-weight: 600;'},
            {field: 'privatecarSurevolume', title: 'Capacity(m3)', style:'font-weight: 600;'},
            
            {field: 'notes', title: 'Remarks'},

            {field: 'settlement', sort: true, title: 'Settlement Party'},
            {field: 'payee', sort: true, title: 'Payee'},
            {field: 'bank', sort: true, title: 'Bank'},
            {field: 'bankAddress', sort: true, title: 'Bank Address'},
            {field: 'bankAccount', sort: true, title: 'Bank Account'},
            {field: 'bankNo', sort: true, title: 'Bank Code'},
            {field: 'cycle', sort: true, title: 'Settlement Cycle'},
            {field: 'taxRate', sort: true, title: 'Tax Rate'},
            {field: 'oilCardNumber', sort: true, title: 'Oil Card Number'},

            {field: 'createTime', title: 'Creation Time'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicPrivatecar.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['privatecarFleetId'] = $("#privatecarFleetId").val();
        queryData['operateId'] = $("#operateId").val();
        table.reload(BasicPrivatecar.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };
    
    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: 'Vehicle Import',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicPrivatecar/toimportExcel',
            end: function () {
                table.reload(BasicPrivatecar.tableId);
            }
        });
    });

    /**
     * 弹出添加对话框
     */
    BasicPrivatecar.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicPrivatecar/add';
    };

    /**
     * 导出excel按钮
     */
    BasicPrivatecar.exportExcel = function () {
        var checkRows = table.checkStatus(BasicPrivatecar.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicPrivatecar.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicPrivatecar/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicPrivatecar.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicPrivatecar/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(BasicPrivatecar.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPrivatecar.tableId,
        url: Feng.ctxPath + '/basicPrivatecar/list',
        page: true,
        limit: 40,
		limits:[60,120,240,360,480,600,2000,5000,20000],
        height: "full-60",
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: BasicPrivatecar.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicPrivatecar.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicPrivatecar.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicPrivatecar.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicPrivatecar.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicPrivatecar.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicPrivatecar.onDeleteItem(data);
        }
    });

    //查询库区
    form.on('select(operateId)',function(){
        $("#privatecarFleetId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"operateId":$("#operateId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Select Carrier</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#privatecarFleetId").html(str);
                }
            }
        });
        form.render('select');
    });
});
